<template>
  <div class="login-container">
    <!-- 左上角logo -->
    <div class="logo-container">
      <img src="/src/assets/logo.png" alt="Logo" class="logo" />
    </div>
    
    <!-- 居中的登录卡片 -->
    <div class="login-card">
      <!-- 左边图片区域 (30%) -->
      <div class="login-image-section">
        <div class="image-text-content">
          <h3 class="image-title">后台管理系统</h3>
          <p class="image-subtitle">提供更前沿的通信能力</p>
          <p class="image-description">让企业与客户沟通更简单、更智能！</p>
        </div>
        <img src="/src/assets/shou.png" alt="Login Image" class="login-image" />
      </div>
      
      <!-- 右边登录表单区域 (70%) -->
      <div class="login-form-section">
        <div class="login-header">
          <h2>后台管理系统</h2>
          <p>请输入超级管理员账号密码登录</p>
        </div>
        
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
        >
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              size="large"
              show-password
            />
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              size="large"
              class="login-button"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { adminLogin } from '@/api/auth'

const router = useRouter()
const loginFormRef = ref()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 调用后端登录接口
    const response = await adminLogin({
      username: loginForm.username,
      password: loginForm.password
    })
    
          // 处理登录成功
      if (response.code === 200) {
        // 保存accessToken到本地存储
        if (response.data && response.data.accessToken) {
          localStorage.setItem('token', response.data.accessToken)
          // 根据role_name设置user_type，如果没有则默认为admin
          localStorage.setItem('user_type', 'admin')
          localStorage.setItem('user_id', response.data.admin_id)
          // 保存其他用户信息
          localStorage.setItem('role_id', response.data.role_id)
          localStorage.setItem('role_name', response.data.role_name)
          localStorage.setItem('full_name', response.data.full_name)
          localStorage.setItem('username', response.data.username)
          localStorage.setItem('permissions', JSON.stringify(response.data.permissions))
          localStorage.setItem('refreshToken', response.data.refreshToken)
          
          // 保存完整的用户信息到userInfo
          const userInfo = {
            admin_id: response.data.admin_id,
            username: response.data.username,
            full_name: response.data.full_name,
            role_id: response.data.role_id,
            role_name: response.data.role_name,
            permissions: response.data.permissions
          }
          localStorage.setItem('userInfo', JSON.stringify(userInfo))
        }
      
      ElMessage.success('登录成功')
      
      // 强制跳转到首页
      try {
        await router.push('/')
      } catch (error) {
        // 如果router.push失败，尝试使用window.location
        window.location.href = '/'
      }
    } else {
      ElMessage.error(response.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error(error.response?.data?.message || '登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.logo-container {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
}

.logo {
  height: 60px;
  width: auto;
}

.login-card {
  width: 1000px;
  height: 600px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.login-image-section {
  width: 35%;
  background: rgb(56, 126, 244);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 40px 30px 0 30px;
  position: relative;
}

.image-text-content {
  color: white;
  text-align: left;
  flex-shrink: 0;
}

.image-title {
  font-size: 28px;
  font-weight: bold;
  margin: 0 0 20px 0;
  color: white;
}

.image-subtitle {
  font-size: 18px;
  margin: 0 0 10px 0;
  color: white;
  opacity: 0.9;
}

.image-description {
  font-size: 16px;
  margin: 0 0 40px 0;
  color: white;
  opacity: 0.8;
  line-height: 1.5;
}

.login-image {
  width: 100%;
  height: auto;
  object-fit: cover;
  display: block;
  flex-shrink: 0;
}

.login-form-section {
  width: 65%;
  padding: 80px 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 28px;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 500;
}

/* 响应式设计 */
@media (max-width: 1100px) {
  .login-card {
    width: 90%;
    height: auto;
    flex-direction: column;
  }
  
  .login-image-section {
    width: 100%;
    height: 250px;
    padding: 30px 20px 0 20px;
  }
  
  .image-title {
    font-size: 24px;
  }
  
  .image-subtitle {
    font-size: 16px;
  }
  
  .image-description {
    font-size: 14px;
  }
  
  .login-form-section {
    width: 100%;
    padding: 40px 30px;
  }
}

@media (max-width: 600px) {
  .logo {
    height: 40px;
  }
  
  .login-form-section {
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .login-header p {
    font-size: 14px;
  }
}
</style> 